﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class CustomerDetail
    {
        partial void Customer_Loaded(bool succeeded)
        {
            this.SetDisplayNameFromEntity(this.Customer);
        }

        partial void Customer_Changed()
        {
            this.SetDisplayNameFromEntity(this.Customer);
        }

        partial void CustomerDetail_Saved()
        {
            this.SetDisplayNameFromEntity(this.Customer);
        }

        partial void CustomerDetail_Activated()
        {
            this.DisplayName = this.Customer.CustomerName + " (Клиент)";

        }

        partial void AddressesAddAndEditNew_Execute()
        {
            this.Application.ShowCreateNewAddress(0,this.Customer.Id,0,0);
        }

        partial void AddressesEditSelected_CanExecute(ref bool result)
        {
            result = (CustomerAddresses.SelectedItem != null);
        }

        partial void AddressesEditSelected_Execute()
        {
            if (CustomerAddresses.SelectedItem != null)
                this.Application.ShowAddressDetail(CustomerAddresses.SelectedItem.Id);
        }

        partial void ShopsEditSelected_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditSalesOrder)
                || this.Application.User.HasPermission(Permissions.CanManageMySalesOrder)
            );
        }

        partial void ShopsEditSelected_Execute()
        {
            if (this.CustomerShops.SelectedItem != null) this.Application.ShowShopDetail(CustomerShops.SelectedItem.Id);
        }

        partial void CustomerDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.

        }

        partial void ShowAuditHistory_Execute()
        {
            this.Application.ShowAuditLogHistory("Клиенты", this.Customer.Id);
        }

        partial void ContactsAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.

        }

        partial void ContactsAddAndEditNew_Execute()
        {
            this.Application.ShowCreateNewContact(this.Customer.Id, 0, 0);
        }

        partial void ShopsAddNew_CanExecute(ref bool result)
        {
            result = (this.Application.User.HasPermission(Permissions.CanEditShop)
                || this.Application.User.HasPermission(Permissions.CanManageMyShop)
            );
        }

        partial void ShopsAddNew_Execute()
        {
            this.Application.ShowCreateNewShop(this.Customer.Id, 0);
        }
    }
}